package com.neusoft.neu24.his.hisapp;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HelloApplication_Combox extends Application {

    private  final   ComboBox<Dept> account = new ComboBox<>();;
    @Override
    public void start(Stage primaryStage) throws IOException {



        Label accountsLabel = new Label("Account:");
        account.setPrefWidth(200);
        Button saveButton = new Button("Save");
        Button addButton = new Button(" Add One ");
        HBox hbox = new HBox(
                accountsLabel,
                account,
                saveButton,
                addButton);
        hbox.setSpacing( 10.0d );
        hbox.setAlignment(Pos.CENTER );
        hbox.setPadding( new Insets(40) );

        Scene scene = new Scene(hbox);

        initCombo();

        saveButton.setOnAction( (evt) -> {

            System.out.println(  account.getSelectionModel().getSelectedIndex());
            System.out.println(  account.getSelectionModel().getSelectedItem() );
            System.out.println(account.getValue());


        });

        addButton.setOnAction( (evt) -> {
//            删除所有
            account.getItems().clear();
            Dept dept = new Dept(40,"采矿学院","采矿馆");
            account.getItems().add(dept);
        });

        primaryStage.setTitle("CombosApp");
        primaryStage.setScene( scene );
        primaryStage.show();

    }

    private void initCombo() {
        List<Dept> depts  = new ArrayList<>();
        depts.add(new Dept(10,"软件学院","loc1"));
        depts.add(new Dept(20,"计算机与信息学院","loc2"));
        depts.add(new Dept(30,"材料学院","loc3"));
        ObservableList<Dept> observableList = FXCollections.observableArrayList(depts);
        account.setItems(observableList);


        account.setConverter(new StringConverter<Dept>() {
            @Override
            public String toString(Dept dept) {
                return  dept==null ?"" :   dept.getDname();
            }

            @Override
            public Dept fromString(String string) {
                return null;
            }
        });





    }

    private void initChoice() {


    }

    public  static  class  Dept{
        private  int deptno =10;
        private String dname;
        private  String loc ;

        public Dept(int deptno, String dname, String loc) {
            this.deptno = deptno;
            this.dname = dname;
            this.loc = loc;
        }


        public int getDeptno() {
            return deptno;
        }

        public void setDeptno(int deptno) {
            this.deptno = deptno;
        }

        public String getDname() {
            return dname;
        }

        public void setDname(String dname) {
            this.dname = dname;
        }

        public String getLoc() {
            return loc;
        }

        public void setLoc(String loc) {
            this.loc = loc;
        }

        @Override
        public String toString() {
            return "Dept{" +
                    "deptno=" + deptno +
                    ", dname='" + dname + '\'' +
                    ", loc='" + loc + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        launch();
    }
}